<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: HelpToc.php 362 2010-02-03 08:41:11Z steen.rabol@gmail.com $
 *
 */

class HelpToc extends PortalSystemPage
{
	private $ditem	= 0;
	public function onInit($param)
	{
		parent::onInit($param);

		if(!$this->User)
		{
			$this->reportError(1,Prado::localize("You need to be logged in to use this page"));
		}

		if($this->User->RoleId != $this->Application->Parameters['AdminRole'])
		{
			$this->reportError(1,Prado::localize("You do not have access to this function/page"));
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Page->Title = Prado::localize("Help TOC");
	}

	public function onPreRender($param)
	{
		parent::onPreRender($param);

		// Publish assets
		$assetManager	= $this->Application->AssetManager;
		$clientScripts	= $this->Page->ClientScript;

		$jsurl = $assetManager->publishFilePath(Prado::getPathOfNamespace('Application.Common.3rdParty.DTree'));
		$clientScripts->registerHeadScriptFile('dtree_assets', $jsurl . '/dtree.js');
		$clientScripts->registerStyleSheetFile('dtree_assets_css', $jsurl . '/dtree.css');

		$imgfolder = $jsurl . '/';

		$script = "";
		$script .= "helptree_d = new dTree('helptree_d','$imgfolder');";
		$script .="helptree_d.add(0,-1,'" . Prado::localize('Help') . "');";

		// Add nodes
		$helpfiles = $this->DataAccess->createCommand("select * from tblhelp where culture='" . $this->Application->Culture . "' order by pagename")->query();
		$tocarray	= array();
		foreach($helpfiles as $helpfile)
		{
			$t = explode('.', $helpfile['pagename']);
			$tocarray[$t[0]][] = array("name" => $helpfile['title'], "url" => $this->Service->constructUrl('System.Help', array('pagename' => $helpfile['pagename'])));
		}

		foreach($tocarray as $key => $value)
		{
			$this->ditem++;
			$script .="helptree_d.add(" . $this->ditem . ",0,'" . $key . "','" . '' . "','" . $key . "');";
			$parent	= $this->ditem;
			foreach($value as $v)
			{
				$this->ditem++;
				$script .="helptree_d.add(" . $this->ditem . ",$parent,'" . $v['name'] . "','" . $v['url'] . "','" . $v['name'] . "');";
			}
		}
		$script .="document.write(helptree_d);\r\n";
		$script .="helptree_d.closeAll();";
		$this->dtHelpTree->Text = $script;
	}
}
?>
